package com.test;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private CheckBox cid1;
    private CheckBox cid2;
    private CheckBox cid3;
    private Button bid;
    private RadioGroup rg;
    private RadioButton rid1;
    private RadioButton rid2;
    private ImageView img;

    private TimePicker timdid;
    private Button timdbid;
    private DatePicker dateid;
    private Button datebid;
    private ProgressBar progressBar;
    private Button barbid1;

    private SeekBar seekBar;

    private Button barbid2;

    private Switch switchid;
    private EditText editText1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        //================================
        OnBoxClickListener b = new OnBoxClickListener();
        OnBoxCheckedChangeListener c = new OnBoxCheckedChangeListener();
        cid1 = (CheckBox) findViewById(R.id.cid1);
        cid2 = (CheckBox) findViewById(R.id.cid2);
        cid3 = (CheckBox) findViewById(R.id.cid3);

        //        cid1.setOnClickListener(c);
        //        cid2.setOnClickListener(c);
        //        cid3.setOnClickListener(c);
        cid1.setOnCheckedChangeListener(c);
        cid2.setOnCheckedChangeListener(c);
        cid3.setOnCheckedChangeListener(c);

        bid = (Button) findViewById(R.id.bid);
        bid.setOnClickListener(b);


        rg = (RadioGroup) findViewById(R.id.rgid);
        rg.setOnCheckedChangeListener(d);


        img = (ImageView) findViewById(R.id.imgid);
        img.setImageResource(R.mipmap.wrc);
        img.setScaleType(ImageView.ScaleType.FIT_CENTER);
        //        rid1 = (RadioButton) findViewById(R.id.rid1);
        //        rid2 = (RadioButton) findViewById(R.id.rid2);
        //        rid1.setOnCheckedChangeListener(c);
        //        rid2.setOnCheckedChangeListener(c);


        timdid = (TimePicker) findViewById(R.id.timeid);
        timdid.setIs24HourView(true);
        timdid.setOnTimeChangedListener(timeChangedListener);
        timdbid = (Button) findViewById(R.id.timdbid);
        timdbid.setOnClickListener(onClickListener);


        dateid = (DatePicker) findViewById(R.id.dateid);
        dateid.updateDate(2011, 00, 01);
        datebid = (Button) findViewById(R.id.datebid);
        datebid.setOnClickListener(onClickListener1);
        progressBar = (ProgressBar) findViewById(R.id.barid1);
        progressBar.setMax(100);
        progressBar.setProgress(10);
        progressBar.setSecondaryProgress(20);

        boolean flag = progressBar.isIndeterminate();
        System.out.println("flag:" + flag);

        barbid1 = (Button) findViewById(R.id.barbid1);
        barbid1.setOnClickListener(onClickListener2);


        seekBar = (SeekBar) findViewById(R.id.barid2);
        seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
        barbid2 = (Button) findViewById(R.id.barbid2);
        barbid2.setOnClickListener(onClickListener3);
        //================================


        switchid = (Switch) findViewById(R.id.switchid);
        switchid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Switch s = (Switch) v;
                System.out.println(s.isChecked());
            }
        });
        //============================
        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                System.out.println("keyCode：" + keyCode);
                Toast.makeText(MainActivity.this, "keyCode：" + keyCode, Toast.LENGTH_SHORT).show();
                //输入银行账号，用大字回显出来字符,每4个字符用横线隔开
                switch (event.getAction()) {
                    case KeyEvent.ACTION_UP:             //键盘松开
                        Log.i("===", "键盘松开" + editText1.getText());
                        break;
                    case KeyEvent.ACTION_DOWN:          //键盘按下
                        Log.i("===", "键盘按下");
                        break;
                }
                return false;
            }
        });

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    View.OnClickListener onClickListener3 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            seekBar.incrementProgressBy(10);
        }
    };

    SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            System.out.println("onProgressChanged:" + progress + "|" + fromUser + "|");
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            System.out.println("onStart..." + seekBar.getProgress());

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            System.out.println("onStop..." + seekBar.getProgress());
        }
    };


    View.OnClickListener onClickListener2 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar.incrementProgressBy(5);
            progressBar.incrementSecondaryProgressBy(8);
        }
    };

    View.OnClickListener onClickListener1 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("ButtonGetDate", dateid.getYear() + "|" + dateid.getMonth() + "|" + dateid.getDayOfMonth());

        }
    };


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("ButtonGetTime", timdid.getCurrentHour() + "|" + timdid.getCurrentMinute());
        }
    };


    TimePicker.OnTimeChangedListener timeChangedListener = new TimePicker.OnTimeChangedListener() {
        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            Log.i("OnTimeChangedListener", hourOfDay + "|" + minute);
        }
    };


    RadioGroup.OnCheckedChangeListener d = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup v, int checkedId) {
            System.out.println("点击了" + checkedId);
            switch (checkedId) {
                case R.id.rid1:
                    break;
                case R.id.rid2:
                    break;
                default:
                    break;
            }
        }
    };

    class OnBoxClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            cid1.setChecked(!cid1.isChecked());
            cid2.setChecked(!cid2.isChecked());
            cid3.setChecked(!cid3.isChecked());
        }

    }

    class OnBoxCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton bv, boolean isChecked) {
            System.out.print("点击了" + bv.getId());
            System.out.println(bv.getTag());
            System.out.println(bv.isChecked());
            System.out.println(isChecked);
            switch (bv.getId()) {
                case R.id.cid1:
                    break;
                case R.id.cid2:
                    break;
                case R.id.cid3:
                    break;
                default:
                    break;
            }
        }
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
